package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.DistributedLock;
import com.sc.nft.dao.ExternalLinkDao;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.ExternalLink;
import com.sc.nft.entity.dto.ExternalLinkPageAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkSaveAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkUpdateAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkWebDTO;
import com.sc.nft.entity.vo.AdminExternalLinkListVO;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ExternalLinkIconService;
import com.sc.nft.service.ExternalLinkService;
import com.sc.nft.service.UserCreatorInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 外链实体表服务接口实现
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-03-03 17:10:48
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExternalLinkServiceImpl implements ExternalLinkService {

    private final ExternalLinkDao externalLinkDao;

    private final UserCreatorInfoService userCreatorInfoService;

    private final ExternalLinkIconService externalLinkIconService;
    private final StaticDataDao staticDataDao;

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DistributedLock(value = {"ExternalLinkService", "deleteById", "#{id}"}, message = "禁止频繁操作")
    public Boolean deleteById(Long id) {
        return externalLinkDao.deleteById(id);
    }

    /**
     * 后台获取用户的外链列表
     *
     * @param externalLinkPageAdminDTO
     * @return
     */
    @Override
    public Page<AdminExternalLinkListVO> pageAdmin(ExternalLinkPageAdminDTO externalLinkPageAdminDTO) {
        return externalLinkDao.pageAdmin(externalLinkPageAdminDTO);
    }

    /**
     * 根据唯一标识ID修改数据
     *
     * @param externalLinkUpdateAdminDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DistributedLock(value = {"ExternalLinkService", "updateById", "#{externalLinkUpdateAdminDTO.id}"}, message = "禁止频繁操作")
    public Boolean updateById(ExternalLinkUpdateAdminDTO externalLinkUpdateAdminDTO) {
        // 根据daoId+iconId+url判定该参数是否存在
        if (externalLinkUpdateAdminDTO.getIconId() != null && !StrUtil.isEmpty(externalLinkUpdateAdminDTO.getUrl())) {
            Boolean urlExist = externalLinkDao.selectByDaoIdUrlAndIconIdExistNotId(externalLinkUpdateAdminDTO.getIconId(),
                    externalLinkUpdateAdminDTO.getUrl(), externalLinkUpdateAdminDTO.getDaoId(),externalLinkUpdateAdminDTO.getId());
            if (urlExist) {
                throw new GlobalRunTimeException("同url已存在");
            }
        }

        // 验证dao是否存在
        extracted(externalLinkUpdateAdminDTO.getDaoId());
        // 用来判断外链地址是否存在
        Long iconId = externalLinkUpdateAdminDTO.getIconId();
        ExternalLinkUpdateAdminDTO newInfoData = new ExternalLinkUpdateAdminDTO();
        // 赋值给一个新的对象数据
        BeanUtil.copyProperties(externalLinkUpdateAdminDTO, newInfoData);
        // 前端给的ID给空 判定对象是不是存在数据,如果不存在的话则是前端没有给修改数据直接调用了
        externalLinkUpdateAdminDTO.setId(null);
        if (BeanUtil.isEmpty(externalLinkUpdateAdminDTO)) {
            throw new GlobalRunTimeException("修改数据不能为空");
        }
        if (iconId != null) {
            Boolean iconExist = externalLinkIconService.selectByIdExist(iconId);
            if (!iconExist) {
                throw new GlobalRunTimeException("选择的外链地址不存在");
            }
        }
        return externalLinkDao.updateByIdAndIconId(newInfoData);
    }

    /**
     * 新增
     * 不允许出现同名同转跳链接的数据.数据接受到以后必须去掉空格
     *
     * @param externalLinkSaveAdminDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DistributedLock(value = {"ExternalLinkService", "save"}, message = "禁止频繁操作")
    public Boolean save(ExternalLinkSaveAdminDTO externalLinkSaveAdminDTO) {
        // 验证dao是否存在
        extracted(externalLinkSaveAdminDTO.getDaoId());
        // 判断外链地址是否存在
        Boolean iconExist = externalLinkIconService.selectByIdExist(externalLinkSaveAdminDTO.getIconId());
        if (!iconExist) {
            throw new GlobalRunTimeException("选择的外链地址不存在");
        }
        // 判断当前url是否已经存在
        Boolean urlExist = externalLinkDao.selectByDaoIdUrlAndIconIdExist(externalLinkSaveAdminDTO.getIconId(), externalLinkSaveAdminDTO.getUrl(), externalLinkSaveAdminDTO.getDaoId());
        if (urlExist) {
            throw new GlobalRunTimeException("同url已存在");
        }
        ExternalLink externalLink = new ExternalLink();
        BeanUtil.copyProperties(externalLinkSaveAdminDTO, externalLink);
        boolean save = externalLinkDao.save(externalLink);
        return save;
    }

    /**
     * 前端根据dao获取所有数据列表
     *
     * @param daoId
     * @return
     */
    @Override
    public Page<AdminExternalLinkListVO> webPage(ExternalLinkWebDTO externalLinkWebDTO) {
        String mtAppId = staticDataDao.getByType("app_id").getValue();
        Page<AdminExternalLinkListVO> adminExternalLinkListVOPage = externalLinkDao.webPage(externalLinkWebDTO);
        List<AdminExternalLinkListVO> records = adminExternalLinkListVOPage.getRecords();
        for (AdminExternalLinkListVO record : records) {
            record.setMtAppId(mtAppId);
        }
        return adminExternalLinkListVOPage;
    }

    /**
     * 根据daoId获取这个作者是否存在.
     *
     * @param daoId
     */
    private void extracted(Long daoId) {
        GetDaoByIdVO daoById = userCreatorInfoService.getDaoById(daoId);
        if (BeanUtil.isEmpty(daoById)) {
            throw new GlobalRunTimeException("当前作者不存在");
        }
//        if (daoById.getStatus().equals(0)) {
//            throw new GlobalRunTimeException("当前作者必须为Dao才可以配置外链转跳");
//        }
    }

}